<?php
/**
 * get one rail_user and parse user.tpl
 *
 * PHP versions 5
 *
 * Copyright (C)
 * This program is free software. You can redistribute it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation. Either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Without even the implied warranty
 * of ERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. Ff not, write to the Free Software
 * Fundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *
 * Copyright (C) 2006-2007
 *
 * @author Bastian Schröder <bs@chemanet.de>
 * @version 2008-04-12
 * @package railusers
 */

function ajax_call( $ajaxBridge ) {
	global $rail, $smarty;
	$user = rail_factory::get_object( $rail, $_GET['user'] );

	// memberships
	$memberships = array();
	foreach( $user->get_memberships() as $group ){
		$memberships[] = array(
			'id' => $group->get_id(),
			'name' => $group->get_name(),
			'desc' => $group->get_attribute( OBJ_DESC ),
			'is_writeable' => $group->is_writeable()
		);
	}

	// all readable rail_group's
	$all_groups = array();
	foreach( rail_factory::search( $rail, array(), 'rail_group' ) as $group ){
		// continue if group isn't readable or user is already a member of this group
		if( !$group->is_writeable() || $group->is_member($user) )
			continue;

		$all_groups[] = array(
			'id' => $group->get_id(),
			'name' => $group->get_name(),
			'desc' => $group->get_attribute( OBJ_DESC )
		);
	}


	// smarty
	$smarty->assign(array(
		'user' => array(
			'id' => $user->get_id(),
			'name' => $user->get_name(),
			'username' => $user->get_attribute( 'username' ),
			'mail' => $user->get_attribute( 'mail' ),
			'group' => $user->get_group()->get_id(),
			'is_writeable' => $user->is_writeable(),

			't_create' => $user->is_readable( $rail->get_user() ) ? date( 'D d.m.Y H:i', $user->get_attribute( OBJ_TIME_CREATE ) ) : '--',
			't_lastwrite' => $user->is_readable( $rail->get_user() ) ? date( 'D d.m.Y H:i', $user->get_attribute( OBJ_TIME_CREATE ) ) : '--'
		),

		'memberships' => $memberships,
		'available_groups' => $all_groups
	));

	// JSON
	$ajaxBridge->append(array(
		'html_code' => $smarty->fetch('user.tpl')
	));
}

?>